﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfStudy
{
    /// <summary>
    /// AnimationTest.xaml 的交互逻辑
    /// </summary>
    public partial class AnimationTest : Window
    {
        DispatcherTimer timer1;

        public AnimationTest()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation dX = new DoubleAnimation(); 
            DoubleAnimation dY = new DoubleAnimation();

            dX.AutoReverse = false;
            dY.AutoReverse = false;
            dX.RepeatBehavior = RepeatBehavior.Forever;
            dY.RepeatBehavior = RepeatBehavior.Forever;

            CubicEase cb = new CubicEase();
            cb.EasingMode = EasingMode.EaseInOut;
            dX.EasingFunction = cb;
            dY.EasingFunction = cb;

            
            //指定地点
            //dX.From = 0D;//this.tt.X;
            //dY.From = 0D;//this.tt.Y;

            Random r = new Random();
            dX.To = 172;
            dY.To = 172;

            BounceEase bounceEase = new BounceEase();
            bounceEase.Bounces = 3;
            bounceEase.Bounciness = 4;
            dY.EasingFunction = bounceEase;

            Duration duration = new Duration(TimeSpan.FromMilliseconds(1000));
            dX.Duration = duration;
            dY.Duration = duration;

            /*this.tt.BeginAnimation(TranslateTransform.XProperty, dX);
            this.tt.BeginAnimation(TranslateTransform.YProperty, dY);*/
            Move.BeginAnimation(Button.WidthProperty, dX);
            Move.BeginAnimation(HeightProperty, dX);


        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DoubleAnimationUsingKeyFrames dakfX = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames dakfY = new DoubleAnimationUsingKeyFrames();

            //设置关键帧动画时长
            dakfX.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
            dakfY.Duration = new Duration(TimeSpan.FromMilliseconds(3000));

            dakfX.AutoReverse = false;
            dakfY.AutoReverse = false;

            //创建，添加关键帧
            LinearDoubleKeyFrame x_KF_1 = new LinearDoubleKeyFrame();
            LinearDoubleKeyFrame x_KF_2 = new LinearDoubleKeyFrame();
            LinearDoubleKeyFrame x_KF_3 = new LinearDoubleKeyFrame();

            x_KF_1.KeyTime = KeyTime.FromPercent(0.33);
            x_KF_1.Value = 100D;
            x_KF_2.KeyTime = KeyTime.FromPercent(0.66);
            x_KF_2.Value = 200D;
            x_KF_3.KeyTime = KeyTime.FromPercent(1.0);
            x_KF_2.Value = 300D;

            dakfX.KeyFrames.Add(x_KF_1);
            dakfX.KeyFrames.Add(x_KF_2);
            dakfX.KeyFrames.Add(x_KF_3);

            LinearDoubleKeyFrame y_KF_1 = new LinearDoubleKeyFrame();
            LinearDoubleKeyFrame y_KF_2 = new LinearDoubleKeyFrame();
            LinearDoubleKeyFrame y_KF_3 = new LinearDoubleKeyFrame();

            y_KF_1.KeyTime = KeyTime.FromPercent(0.33);
            y_KF_1.Value = 100D; 
            y_KF_2.KeyTime = KeyTime.FromPercent(0.66);
            y_KF_2.Value = 200D;
            y_KF_3.KeyTime = KeyTime.FromPercent(1.0);
            y_KF_2.Value = 300D;

            dakfY.KeyFrames.Add(y_KF_1);
            dakfY.KeyFrames.Add(y_KF_2);
            dakfY.KeyFrames.Add(y_KF_3);

            mybutton.BeginAnimation(Button.WidthProperty, dakfX);
            mybutton.BeginAnimation(HeightProperty, dakfY);

            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation dX = new DoubleAnimation();
            DoubleAnimation dY = new DoubleAnimation();

            dX.AutoReverse = false;
            dY.AutoReverse = false;
            dX.RepeatBehavior = RepeatBehavior.Forever;
            dY.RepeatBehavior = RepeatBehavior.Forever;

            CircleEase cb = new CircleEase();
            cb.EasingMode = EasingMode.EaseOut;
            dX.EasingFunction = cb;
            dY.EasingFunction = cb;


            //指定地点
            //dX.From = 0D;//this.tt.X;
            //dY.From = 0D;//this.tt.Y;

            Random r = new Random();
            dX.To = 172;
            dY.To = 172;

            /*BounceEase bounceEase = new BounceEase();
            bounceEase.Bounces = 3;
            bounceEase.Bounciness = 4;
            dY.EasingFunction = bounceEase;*/

            Duration duration = new Duration(TimeSpan.FromMilliseconds(4000));
            dX.Duration = duration;
            dY.Duration = duration;

            /*this.tt.BeginAnimation(TranslateTransform.XProperty, dX);
            this.tt.BeginAnimation(TranslateTransform.YProperty, dY);*/
            /*myEllipse.BeginAnimation(WidthProperty, dX);
            myEllipse.BeginAnimation(HeightProperty, dX);*/

            //创建场景
            Storyboard storyboard = new Storyboard();
            Storyboard.SetTarget(dX, myEllipse);
            Storyboard.SetTarget(dY, myEllipse);
            Storyboard.SetTargetProperty(dX, new PropertyPath(WidthProperty));
            Storyboard.SetTargetProperty(dY, new PropertyPath(HeightProperty));

            storyboard.Duration = Duration.Forever;
            storyboard.Children.Add(dX);
            storyboard.Children.Add(dY);
            storyboard.Begin(this);

            EllipseAnimation();

            timer1 = new DispatcherTimer();
            timer1.Interval = TimeSpan.FromMilliseconds(800);
            timer1.Tick += Timer_Elapsed1;
            timer1.Start();
        }

        private void Timer_Elapsed1(object sender, EventArgs e)
        {
            new EllipseControl(AnimationPanel);
        }

        public void EllipseAnimation()
        {
            EllipseControl ec = new EllipseControl(AnimationPanel);
        }

        public class EllipseControl
        {
            private Grid parentPanel = null;
            public Ellipse ellipse = null;
            public double ToWidth = 240;

            Storyboard sb = null;

            public EllipseControl(Grid grid)
            {
                this.parentPanel = grid;
                ellipse = new Ellipse();
                ellipse.Height = 40;
                ellipse.Width = 40;
                LinearGradientBrush lgb = new LinearGradientBrush((Color)ColorConverter.ConvertFromString("#FF8952FF"), (Color)ColorConverter.ConvertFromString("#FF34CCFF"), new Point(1, 0.5),new Point(0, 0.5));
                ellipse.Stroke = lgb;
                parentPanel.Children.Add(ellipse);
                AnimationEllips(ellipse.Width, 1);
            }

            public void AnimationEllips(double fromWith, double fromOpcity, double time = 3)
            {
                sb = new Storyboard();

                DoubleAnimation daW = new DoubleAnimation();
                daW.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                daW.From = fromWith;
                daW.To = ToWidth;
                daW.Duration = TimeSpan.FromSeconds(time);
                Storyboard.SetTarget(ellipse, daW);
                Storyboard.SetTargetProperty(daW, new PropertyPath(WidthProperty));
                sb.Children.Add(daW);

                DoubleAnimation daH = new DoubleAnimation();
                daH.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                daH.From = fromWith;
                daH.To = ToWidth;
                daH.Duration = TimeSpan.FromSeconds(time);
                Storyboard.SetTarget(ellipse, daH);
                Storyboard.SetTargetProperty(daH, new PropertyPath(HeightProperty));
                sb.Children.Add(daH);

                DoubleAnimation daO = new DoubleAnimation();
                daO.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                daO.From = fromOpcity;
                daO.To = 0;
                daO.Duration = TimeSpan.FromSeconds(time);
                Storyboard.SetTarget(ellipse, daO);
                Storyboard.SetTargetProperty(daO, new PropertyPath(OpacityProperty));
                sb.Children.Add(daO);

                sb.Completed += (object sender, EventArgs e) => parentPanel.Children.Remove(ellipse);

                sb.Begin(ellipse, HandoffBehavior.Compose);
            }


        }
    }
}
